﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SCMS.UI.Models;
using SCMS.Reports;

namespace SCMS.UI.Areas.Reports.Controllers
{
    public class TAFController : Controller
    {
        //
        // GET: /Reports/TAF/

        public ActionResult Index(Guid TAFid)
        {
            var model = TAFExtention.PrepareTAF(TAFid);
            Dictionary<String, String> data = new Dictionary<string, string>();

            data.Add("{REQ_NO.}", model.TAuthFormEntity.RefNumber);
            data.Add("{DATE}", model.TAuthFormEntity.PreparedOn.Value.ToString("dd/MM/yyyy"));
            data.Add("{TRAVELLER}", (model.Travelller != null) ? model.Travelller.StaffName : model.TAuthFormEntity.TravelerName);
            data.Add("{POSITION}", (model.Travelller != null) ? model.Travelller.Designation : model.TAuthFormEntity.TravelerTitle);

            data.Add("{PURPOSE}", model.TAuthFormEntity.TravelPurpose);
            data.Add("{DESTINATION}", model.TAuthFormEntity.Destination);

            data.Add("{DOMESTIC}", model.TAuthFormEntity.TravellType == "Domestic" ? "\" checked=\"checked\"" : "");
            data.Add("{INTERNATIONAL}", model.TAuthFormEntity.TravellType == "International" ? "\" checked=\"checked\"" : "");

            data.Add("{EMPLOYEE}", model.TAuthFormEntity.TravellerType == "Employee" ? "\" checked=\"checked\"" : "");
            data.Add("{CONSULTANT}", model.TAuthFormEntity.TravellerType == "Consultant" ? "\" checked=\"checked\"" : "");
            data.Add("{PATICIPANT}", model.TAuthFormEntity.TravellerType == "Activity Participant" ? "\" checked=\"checked\"" : "");

            data.Add("{ROAD}", model.TAuthFormEntity.TravellMeans == "Road" ? "\" checked=\"checked\"" : "");
            data.Add("{AIR}", model.TAuthFormEntity.TravellMeans == "Air" ? "\" checked=\"checked\"" : "");
            data.Add("{SEA}", model.TAuthFormEntity.TravellMeans == "Sea" ? "\" checked=\"checked\"" : "");

            data.Add("{FROM-DATE}", model.TAuthFormEntity.TravelStartDate.Value.ToString("dd/MM/yyyy"));
            data.Add("{TO-DATE}", model.TAuthFormEntity.TravelEndDate.Value.ToString("dd/MM/yyyy"));
            data.Add("{NO-OF-NIGHTS}", model.TAuthFormEntity.NoOfNights.ToString());

            data.Add("{FROM-LOC}", model.TAuthFormEntity.FromLocation);
            data.Add("{TO-LOC}", model.TAuthFormEntity.ToLocation);
            data.Add("{VIA-LOC}", model.TAuthFormEntity.ViaLocation);


            data.Add("{REMARKS}", model.TAuthFormEntity.Remarks);
            TravelDates(model, data);

            TravelExpenses(model, data);

            data.Add("{TAFVALUE}", "");// model.MBCurrency);
            data.Add("{TAFCURRENCY}", model.TAuthFormEntity.Currency.ShortName);
            data.Add("{MBCURRENCY}", "");
            data.Add("{TOTALMBVALUE}", "");

            String authourisation = "";
            authourisation = AuthName(model, authourisation);
            authourisation = AuthTitle(model, authourisation);
            authourisation = AuthDate(model, authourisation);
            authourisation = AuthSignature(model, authourisation);
            data.Add("{AUTHORISATION}", authourisation);

            List<String> options = new List<string>();
            options.Add(" --copies 2 ");
            Byte[] output = WkHtml2Pdf.CreateReport(data, "Travel_Authorisation.htm", options);

            return File(output, "application/pdf", "TAF_" + DateTime.Now.FormatDDMMMYYYYHHmm());
        }

        private static void TravelExpenses(ViewTAForms model, Dictionary<String, String> data)
        {
            int expensecount = 1;
            String ExpensedetailHtml = "";
            foreach (var detail in model.TravelExpenses)
            {
                expensecount++;
                ExpensedetailHtml += "<tr><td>" + detail.ExpenseDescription + "</td><td align=\"right\">";
                ExpensedetailHtml += detail.TotalCost.Value.ToString("##,##0.00");
                ExpensedetailHtml += "</td><td align=\"right\">";
                ExpensedetailHtml += detail.AdvanceRequired.Value.ToString("##,##0.00");
                ExpensedetailHtml += "</td><td >";
                ExpensedetailHtml += detail.Currency.ShortName;
                ExpensedetailHtml += "</td><td >";
                ExpensedetailHtml += detail.ProjectBudget.BudgetCategory.ProjectDonor.ProjectNumber;
                ExpensedetailHtml += "</td><td >";
                ExpensedetailHtml += detail.ProjectBudget.LineNumber;
                ExpensedetailHtml += "</td><tr>";
            }
            while (expensecount < 5)
            {
                expensecount++;
                ExpensedetailHtml += "<tr><td></td><td align=\"right\">";
                ExpensedetailHtml += "</td><td align=\"right\">";
                ExpensedetailHtml += "</td><td >&nbsp;</td><td >";
                ExpensedetailHtml += "</td><td ></td><tr>";
            }
            data.Add("{EXPENSEDTAILS}", ExpensedetailHtml);
        }

        private static void TravelDates(ViewTAForms model, Dictionary<String, String> data)
        {

            int count = 1;
            String TravelDatesdetailHtml = "";
            foreach (var detail in model.TravelDates)
            {
                count++;
                TravelDatesdetailHtml += "<tr><td align=\"center\">" + detail.TravalDate.Value.ToShortDateString() + "</td><td>";
                TravelDatesdetailHtml += detail.Destination;
                TravelDatesdetailHtml += "</td><td>";
                TravelDatesdetailHtml += detail.NoOfNights;
                TravelDatesdetailHtml += "</td><td align=\"right\">";
                TravelDatesdetailHtml += detail.DailyPDRate.Value.ToString("##,##0.00");
                TravelDatesdetailHtml += "</td><td align=\"right\">";
                TravelDatesdetailHtml += detail.TotalAllowance.Value.ToString("##,##0.00");
                TravelDatesdetailHtml += "</td><td align=\"right\">";
                TravelDatesdetailHtml += detail.AdvanceRequired.Value.ToString("##,##0.00");
                TravelDatesdetailHtml += "</td><td>";
                TravelDatesdetailHtml += detail.Currency.ShortName;
                TravelDatesdetailHtml += "</td><td>";
                TravelDatesdetailHtml += detail.ProjectBudget.BudgetCategory.ProjectDonor.ProjectNumber;
                TravelDatesdetailHtml += "</td><td>";
                TravelDatesdetailHtml += detail.ProjectBudget.LineNumber;
                TravelDatesdetailHtml += "</td><tr>";
            }
            while (count < 5)
            {
                count++;
                TravelDatesdetailHtml += "<tr><td align=\"center\"></td><td>&nbsp;</td><td>";
                TravelDatesdetailHtml += "</td><td align=\"right\">";
                TravelDatesdetailHtml += "</td><td align=\"right\">";
                TravelDatesdetailHtml += "</td><td align=\"right\">";
                TravelDatesdetailHtml += "</td><td></td><td>";
                TravelDatesdetailHtml += "</td><td></td><tr>";

            }
            data.Add("{TRAVELDATES}", TravelDatesdetailHtml);
        }

        private static String AuthSignature(ViewTAForms model, String authourisation)
        {

            authourisation += "<tr><th>Signature:</th><td>";
            if (model.PreparedBy != null && model.PreparedBy.SignatureImage.IsNotNull())
                authourisation += string.Format("<img src=\"{0}\" alt=\"\" style=\"max-width: 100px;\" />", "data:image/png;base64," + Convert.ToBase64String(model.PreparedBy.SignatureImage));
            authourisation += "</td><td>";
            if (model.ApprovedBy != null && model.ApprovedBy.SignatureImage.IsNotNull())
                authourisation += string.Format("<img src=\"{0}\" alt=\"\" style=\"max-width: 100px;\" />", "data:image/png;base64," + Convert.ToBase64String(model.ApprovedBy.SignatureImage));
            authourisation += "</td><td>";
            if (model.AuthorizedBy != null && model.AuthorizedBy.SignatureImage.IsNotNull())
                authourisation += string.Format("<img src=\"{0}\" alt=\"\" style=\"max-width: 100px;\" />", "data:image/png;base64," + Convert.ToBase64String(model.AuthorizedBy.SignatureImage));
            authourisation += "</td><tr>";
            return authourisation;
        }

        private static String AuthDate(ViewTAForms model, String authourisation)
        {

            authourisation += "<tr><th>Date:</th><td>";
            authourisation += model.TAuthFormEntity.PreparedOn.Value.ToString("dd/MM/yyyy h:mm tt");
            authourisation += "</td><td>";
            if (model.ApprovedBy != null)
                authourisation += model.TAuthFormEntity.ReviewedOn.Value.ToString("dd/MM/yyyy h:mm tt");
            authourisation += "</td><td>";
            if (model.AuthorizedBy != null)
                authourisation += model.TAuthFormEntity.AuthorizedOn.Value.ToString("dd/MM/yyyy h:mm tt");
            authourisation += "</td><tr>";
            return authourisation;
        }

        private static String AuthTitle(ViewTAForms model, String authourisation)
        {

            authourisation += "<tr><th>Title:</th><td>";
            if (model.PreparedBy != null)
                authourisation += model.PreparedBy.Designation;
            authourisation += "</td><td>";
            if (model.ApprovedBy != null)
                authourisation += model.ApprovedBy.Designation;
            authourisation += "</td><td>";
            if (model.AuthorizedBy != null)
                authourisation += model.AuthorizedBy.Designation;
            authourisation += "</td><tr>";
            return authourisation;
        }

        private static String AuthName(ViewTAForms model, String authourisation)
        {

            authourisation += "<tr><th>Name:</th><td>";
            if (model.PreparedBy != null)
                authourisation += model.PreparedBy.StaffName;
            authourisation += "</td><td>";
            if (model.ApprovedBy != null)
                authourisation += model.ApprovedBy.StaffName;
            authourisation += "</td><td>";
            if (model.AuthorizedBy != null)
                authourisation += model.AuthorizedBy.StaffName;
            authourisation += "</td><tr>";

            return authourisation;
        }

    }
}
